<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>

<%@ page import="board1.BoardDAO"%>
<%@ page import="board1.BoardTO"%>
<%@ page import="board1.ReplyDAO"%>
<%@ page import="board1.ReplyTO"%>
<%@ page import="board1.UserDAO"%>
<%@ page import="board1.UserTO"%>
<%@ page import="java.util.ArrayList"%>

<%
	request.setCharacterEncoding("UTF-8");

	// 	int cpage = 1;
	// 	if (request.getParameter("cpage") != null
	// 			&& !request.getParameter("cpage").equals("")) {
	// 		cpage = Integer.parseInt(request.getParameter("cpage"));
	// 	}

	BoardDAO dao = new BoardDAO();

	String idx = request.getParameter("idx");

	BoardTO to = dao.viewBoard(idx);

	String board_list = "";

	// 	for (int i = 0; i < data.size(); i++) {
	// 		BoardTO to = (BoardTO) data.get();

	String phone_number = to.getPhone_number();
	String chon_id = to.getChon_id();
	String title = to.getTitle();
	String content = to.getContent();
	String wr_dt = to.getWr_dt();
	String pic_names = to.getPic_name();
	// 		String idx = to.getIdx();

	//댓글 json 문자열 시작 ----

	ReplyDAO rep_dao = new ReplyDAO();

	UserDAO user_dao = new UserDAO();

	ArrayList<ReplyTO> rep_data = rep_dao.listReply(idx);

	String reply_list = "";

	UserTO user_to = new UserTO();

	for (int re_i = 0; re_i < rep_data.size(); re_i++) {
		ReplyTO rep_to = (ReplyTO) rep_data.get(re_i);
		String rep_phone_number = rep_to.getPhone_number();

		String rep_idx = to.getIdx();
		String subcontent = rep_to.getSubcontent();

		user_to.setPhone_number(rep_phone_number);

		user_to = (UserTO) user_dao.viewUser(user_to);

		//`   |
		if (re_i == 0) {
			reply_list = user_to.getUser_name() + "`" + subcontent;

		} else {
			reply_list += "|" + user_to.getUser_name() + "`"
					+ subcontent;
		}
	}
 
	user_to.setPhone_number(phone_number);

	user_to = (UserTO) user_dao.viewUser(user_to);

	board_list = "{" + "\"idx\":\"" + idx + "\"," + "\"phone_number\""
			+ ":\"" + phone_number + "\"," + "\"chon_id\"" + ":\""
			+ chon_id + "\"," + "\"title\"" + ":\"" + title + "\","
			+ "\"content\"" + ":\"" + content + "\"," + "\"wr_dt\""
			+ ":\"" + wr_dt + "\"," + "\"pic_names\"" + ":\"" + pic_names
			+ "\"," + "\"user_name\"" + ":\"" + user_to.getUser_name()
			+ "\", \"subcontent\":\"" + reply_list + "\"}";
			
	if (phone_number != null)
		board_list = "[" + board_list + "]";
	else
		board_list = "FAIL";

	out.println(board_list);
%>
